package cn.iocoder.yudao.module.cont.controller.admin.base;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cont.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.PayInfoDO;
import cn.iocoder.yudao.module.cont.service.base.PayInfoService;

@Tag(name = "管理后台 - 合同收付款记录表 ")
@RestController
@RequestMapping("/cont/pay-info")
@Validated
public class PayInfoController {

    @Resource
    private PayInfoService payInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建合同收付款记录表 ")
    @PreAuthorize("@ss.hasPermission('cont:pay-info:create')")
    public CommonResult<Long> createPayInfo(@Valid @RequestBody PayInfoSaveReqVO createReqVO) {
        return success(payInfoService.createPayInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同收付款记录表 ")
    @PreAuthorize("@ss.hasPermission('cont:pay-info:update')")
    public CommonResult<Boolean> updatePayInfo(@Valid @RequestBody PayInfoSaveReqVO updateReqVO) {
        payInfoService.updatePayInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同收付款记录表 ")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cont:pay-info:delete')")
    public CommonResult<Boolean> deletePayInfo(@RequestParam("id") Long id) {
        payInfoService.deletePayInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同收付款记录表 ")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cont:pay-info:query')")
    public CommonResult<PayInfoRespVO> getPayInfo(@RequestParam("id") Long id) {
        PayInfoDO payInfo = payInfoService.getPayInfo(id);
        return success(BeanUtils.toBean(payInfo, PayInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同收付款记录表 分页")
    @PreAuthorize("@ss.hasPermission('cont:pay-info:query')")
    public CommonResult<PageResult<PayInfoRespVO>> getPayInfoPage(@Valid PayInfoPageReqVO pageReqVO) {
        PageResult<PayInfoDO> pageResult = payInfoService.getPayInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PayInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合同收付款记录表  Excel")
    @PreAuthorize("@ss.hasPermission('cont:pay-info:export')")
    @OperateLog(type = EXPORT)
    public void exportPayInfoExcel(@Valid PayInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PayInfoDO> list = payInfoService.getPayInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合同收付款记录表 .xls", "数据", PayInfoRespVO.class,
                        BeanUtils.toBean(list, PayInfoRespVO.class));
    }

}